﻿using System;
using Handrise.Mobile.BasicModal.Config;
using System.Collections.Generic;
using System.Text;

namespace Handrise.Mobile.BasicModal.GPS
{
    public class GpsPositionFormat
    {
        private static ConfigService config = new ConfigService();

        /// <summary>
        /// 枚举类型
        /// </summary>
        internal static PositionTypeEnum positionType = PositionTypeEnum.DOUBLE;

        internal static PositionTypeEnum PositionType
        {
            get
            {
                string strPosition = "";
                if (config != null)
                    strPosition = GpsPositionFormat.config.GetParameter("GPS","GpsPositionType");
                if (strPosition != null && strPosition != "" && strPosition != string.Empty)
                {
                    if (Enum.IsDefined(typeof(PositionTypeEnum), strPosition))
                    {
                        positionType = (PositionTypeEnum)Enum.Parse(typeof(PositionTypeEnum), strPosition, true);
                    }
                }
                return positionType;
            }
        }

        /// <summary>
        /// 将度分秒格式转换为浮点
        /// </summary>
        /// <param name="dmsPosition"></param>
        /// <returns></returns>
        public static double ToDouble(DMSPosition dmsPosition)
        {
            if (dmsPosition.Degrees == 0)
                return 0;
            int absDegrees = Math.Abs(dmsPosition.Degrees);

            double val = (double)absDegrees + ((double)dmsPosition.Minutes / 60.0) + ((double)dmsPosition.Seconds / 3600.0);

            return val * (absDegrees / dmsPosition.Degrees);
        }

        /// <summary>
        /// 将浮点装换为度分秒
        /// </summary>
        /// <param name="doublePosition"></param>
        /// <returns></returns>
        public static DMSPosition ToDMS(double doublePosition)
        {
            int degrees = (int)doublePosition;

            double doubleMinutes = (Math.Abs(doublePosition) - Math.Abs((double)degrees)) * 60.0;

            int minutes = (int)doubleMinutes;
            double seconds = (doubleMinutes - (double)minutes) * 60.0;
            return new DMSPosition(degrees, (int)doubleMinutes, seconds);
        }

        /// <summary>
        /// 将浮点形式的度分秒转换为度分秒类
        /// </summary>
        /// <param name="doublePosition"></param>
        /// <returns></returns>
        public static DMSPosition ParseDMSPosition(double doublePosition)
        {
            double degrees = (doublePosition / 100.0);
            double minutes = (Math.Abs(degrees) - Math.Abs((double)(int)(degrees))) * 100;
            double seconds = (Math.Abs(doublePosition) - Math.Abs((double)(int)doublePosition)) * 60.0;

            return new DMSPosition((int)degrees, (int)minutes, seconds);
        }
    }
}
